package com.chinasoft.workmanage.login.controller;

import com.chinasoft.workmanage.common.base.controller.BaseController;
import com.chinasoft.workmanage.login.model.User;
import com.chinasoft.workmanage.login.service.LoginService;
import org.apache.log4j.Logger;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@Controller
@RequestMapping("/")
public class LoginController extends BaseController {

    private Logger logger = Logger.getLogger(this.getClass());
    @Autowired
    private LoginService loginService;

    @RequestMapping(value = "/login",method ={ RequestMethod.POST,RequestMethod.GET})
    public void login(@RequestBody User user, HttpServletRequest request, HttpServletResponse response) {
        try {
            Map parameterMap = request.getParameterMap();
            JSONObject json = new JSONObject();
            if(user==null || user.getLoginId()==null || user.getPassword()==null){
                json.put("code","001");
                json.put("message","用户名不能为空");
                this.renderJson(json.toString(),request,response);
                return ;
            }
            boolean login = loginService.login(user);
            if(login){
                json.put("code","000");
                json.put("message","登陆成功");
            }else{
                json.put("code","002");
                json.put("message","用户名或密码错误");
            }
            this.renderJson(json.toString(),request,response);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

}
